<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 20px">
        <el-table border :data="tableData" stripe>
          <el-table-column label="序号" type="index" :index="indexMethod" width="100" />
          <el-table-column label="姓名" prop="username" />
          <!-- 头像列最后来处理 -->
          <!-- <el-table-column label="头像" prop="staffPhoto" /> -->
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <!--使用插槽的方式
           <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ row.formOfEmployment === 1 ? '正式' : '临时工' }}
            </template>
          </el-table-column> -->
          <el-table-column :formatter="formatter" label="聘用形式" prop="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delUser(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          style="margin-top: 20px"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
      <!-- 添加员工 -->
      <add-employee :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>
<script>
import { reqDelEmployee, reqGetEmployeesList } from '@/api/employees'
import AddEmployee from './components/AddEmployee.vue'
export default {
  name: 'Employees',
  components: { AddEmployee },
  data() {
    return {
      page: 1,
      size: 5,
      tableData: [],
      total: 0,
      showDialog: false
    }
  },
  created() {
    this.getEmployeesList()
  },
  methods: {
    //   渲染员工列表
    async getEmployeesList() {
      const { data: { total, rows }} = await reqGetEmployeesList(this.page, this.size)
      this.tableData = rows
      this.total = total
    },
    // size发送变化触发的方法
    handleSizeChange(newVal) {
      this.size = newVal
      //   this.page = 1
      this.getEmployeesList()
    },
    // 页码发生变化触发的方法
    handleCurrentChange(newVal) {
      this.page = newVal
      this.getEmployeesList()
    },
    // 自定义索引列
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    formatter(row, column, cellValue, index) {
      return cellValue === 1 ? '正式' : '非正式'
    },
    // 删除员工
    async delUser(id) {
      // 确认提示
      const confirm = await this.$confirm('是否删除此员工', {
        type: 'warning'
      }).catch(() => {})
      if (confirm !== 'confirm') return this.$message.info('取消操作')
      await reqDelEmployee(id)
      if (this.tableData.length === 1 && this.page > 1) {
        this.page--
      }
      this.getEmployeesList()
    }
  }
}
</script>
